﻿@model PagedList<Sys_Admin>
@{
    ViewBag.ParentUrl = "/Manager/Index";
    ViewBag.Title = "用户管理";
    List<Sys_Role> roleList= ViewBag.RoleList;
}
@section breadcrumb
{
    <ul class="breadcrumb">
        <li>
            <i class="ace-icon fa fa-home home-icon"></i>
            <a href="/">控制台</a>
        </li>
        <li class="active">@ViewBag.Title</li>
    </ul><!-- .breadcrumb -->
}
<div class="row">
    <div class="col-xs-12">
        <div class="clearfix">
            <div class="pull-right">
                <a class="btn btn-white btn-info btn-round" href="/Manager/Edit">
                    <i class="ace-icon fa fa-plus bigger-120 blue"></i>
                    添加
                </a>
            </div>
        </div>
        <div class="space-4"></div>

        <div class="table-responsive">
            <!-- PAGE CONTENT BEGINS -->
            <table id="sample-table-1" class="table table-striped table-bordered table-hover">
                <thead>
                    <tr>
                        <th>用户名</th>
                        <th>姓名</th>
                        <th>性别</th>
                        <th>联系方式</th>
                        <th>状态</th>
                        <th>超管?</th>
                        <th>角色备注</th>
                        <th>注册时间</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var item in Model)
                    {
                        <tr>
                            <td>@item.A_Account</td>
                            <td>@item.A_Name</td>
                            <td>@Enum.GetName(typeof(SexEnum), item.A_Sex) </td>
                            <td>@item.A_Phone</td>
                            <td>@UtilPartialView.YesOrNo("是", "否", item.A_Status == 1)</td>
                            <td>@UtilPartialView.YesOrNo("是", "否", item.A_IsAdmin == 0)</td>
                            <td>@item.A_RoleStr</td>
                            <td>@item.A_Time</td>
                            <td>
                                @if (item.A_Status == 1)
                                {
                                    <div class="btn-group">
                                        <button class="btn btn-xs btn-success tooltip-success btnOption" data-rel="tooltip" data-placement="top" title="解冻帐号"   data-val="@item.A_Id">
                                            解冻
                                            <i class="ace-icon fa fa-unlock bigger-120"></i>
                                        </button>
                                    </div>
                                }
                                else if (item.A_Status == 0)
                                {
                                    <div class="btn-group">
                                        <button class="btn btn-xs btn-danger tooltip-error btnOption" data-rel="tooltip" data-placement="top" title="冻结帐号"   data-val="@item.A_Id">
                                            冻结
                                            <i class="ace-icon fa fa-lock bigger-120"></i>
                                        </button>
                                    </div>

                                }
                                <div class="btn-group">
                                    <a class="btn btn-xs btn-info tooltip-info" data-rel="tooltip" data-placement="top" title="冻结帐号" href="/manager/edit/@item.A_Id">
                                        编辑
                                        <i class="ace-icon fa fa-edit bigger-120"></i>
                                    </a>
                                </div>

                                <div class="btn-group">
                                    <button class="btn btn-xs btn-default tooltip-info btnResetPwd" data-rel="tooltip" data-placement="top" title="重置密码"  data-val="@item.A_Id">
                                        重置密码
                                        <i class="ace-icon fa fa-key bigger-120"></i>
                                    </button>
                                </div>

                                <div class="btn-group">
                                    <button class="btn btn-xs btn-inverse tooltip-info btnRole" data-rel="tooltip" data-placement="top" title="角色管理"  data-id="@item.A_Id" data-roles="@item.A_RoleStr">
                                        角色管理
                                        <i class="ace-icon fa fa-users bigger-120"></i>
                                    </button>
                                </div>
                            </td>
                        </tr>
                    }
                </tbody>
            </table>
        </div>

        @Html.Pager(Model, new PagerOptions { FirstPageText = "首页", PrevPageText = "上一页", NextPageText = "下一页", LastPageText = "末页", PageIndexParameterName = "id", NumericPagerItemCount = 5, ContainerTagName = "ul", CssClass = "pagination", CurrentPagerItemTemplate = "<li class=\"active\"><a href=\"#\">{0}</a></li>", DisabledPagerItemTemplate = "<li class=\"disabled\"><a>{0}</a></li>", PagerItemTemplate = "<li>{0}</li>", Id = "mvcpager" })
        <!-- PAGE CONTENT ENDS -->
    </div><!-- /.col -->
</div><!-- /.row -->
<div class="modal fade" id="roleModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title">用户角色</h4>
            </div>
            <div class="modal-body">
                <p></p>
                @Html.Hidden("hidId")
                @foreach (var item in roleList)
                {
                    <label>
                        <input type="checkbox" name="chkRole" id="chkRole1" class="ace" value="@item.R_Id"  data-disname="@item.R_DisplayName"/>
                        <span class="lbl"> @item.R_DisplayName</span>
                    </label>
                }

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" id="btnSaveRole">保存</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

@section footer{
    <script src="/assets/js/bootbox.min.js"></script>
    <script>
        $(function() {
            //冻结
            $('.btnOption').on('click', function() {
                $.post('/Manager/Forzen', { id: $(this).data('val') }, function(o) {
                    if (o.State == 0) {
                        bootbox.alert("操作成功！", function() {
                            //window.location.reload();
                            location.reload();
                        });
                    } else {
                        var ms = o.Message || "操作有误！";
                        bootbox.alert(ms);
                    }
                });
            });
            //设置角色
            $('.btnRole').on('click', function() {
                var roles = $(this).data('roles').split(',');
                $('input[name="chkRole"]').each(function(i, o) {
                    //console.log($(o).prop("checked", true));
                    $(this).prop("checked", $.inArray($(o).data('disname'), roles) > -1);
                });
                $('#hidId').val($(this).data('id'));
                $('#roleModal').modal("show");
            });
            //保存设置的角色
            $('#btnSaveRole').on('click', function() {
                var checkedChk = $('input:checkbox:checked').map(function() {
                    return { k: $(this).val(), v: $(this).data('disname') };
                }).get();
               // console.log(JSON.stringify(checkedChk));
               // return;
                $.post('/Manager/SaveRole', { id: $('#hidId').val(), roleStr: JSON.stringify(checkedChk) }, function (o) {
                    if (o.State == 0) {
                        bootbox.alert("操作成功！", function() {
                            //window.location.reload();
                            location.reload();
                        });
                    } else {
                        var ms = o.Message || "操作有误！";
                        bootbox.alert(ms);
                    }
                });
            });
            //重置密码
            $('.btnResetPwd').on('click', function() {
                $.post('/Manager/ResetPwd', { id: $(this).data('val') }, function(o) {
                    if (o.State == 0) {
                        bootbox.alert("操作成功！", function() {
                            //window.location.reload();
                            location.reload();
                        });
                    } else {
                        var ms = o.Message || "操作有误！";
                        bootbox.alert(ms);
                    }
                });
            });


        });
    </script>
}